--[[
   @brief    view for wegame banner 
   @author   harleyxwang
   @version  1.0
   @date     2017-04-21
--]]


ierd_tgp.tpf_ui.Require('event_center.lua');
ierd_tgp.tpf_ui.Require('wegame_banner\\banner_list.lua');
ierd_tgp.tpf_ui.Require("wegame_banner\\game_off_shell_tips.lua");
ierd_tgp.tpf_ui.Require("wegame_banner\\game_update_lack_space_tips.lua");

BANNER_VIEW_RES = 
{
    BANNER_GROUP_EXPAND_INDICATOR = "ONEIMAGE:banner_list\\expand.png;Stretch;null;";
    BANNER_GROUP_COLLAPSE_INDICATOR = "ONEIMAGE:banner_list\\pack_up.png;Stretch;null;";
};

banner_view =
{
    ub_game_list_panel_id_ = 0;
    is_first_init = true;
    has_get_game_list = false;
    get_list_time_out = 20 * 1000;
    off_shell_tips_wnd_id = 0;
    more_menu_btn_id_ = 0;
    ptr_list_cover_mask_ = nil;

    --parent_ctrl_height = 0;
    switch_ctrl_height = 30;

    game_list_ctrl_height_check_count = 0;

    game_list_ctrl_id = Tenio.INVALID_CONTROLID;
    my_game_panel_ctrl_id = Tenio.INVALID_CONTROLID;
    my_game_list_switcher_id = Tenio.INVALID_CONTROLID;
    my_game_list_ctrl_id = Tenio.INVALID_CONTROLID;
    no_game_panel_ctrl_id = Tenio.INVALID_CONTROLID;
    showing_no_game = false;

    subscription_panel_ctrl_id = Tenio.INVALID_CONTROLID;
    subscription_list_switcher_id = Tenio.INVALID_CONTROLID;
    subscription_list_ctrl_id = Tenio.INVALID_CONTROLID;
    subscription_indicator_id = Tenio.INVALID_CONTROLID;

    init = function()
        banner_view.inner_log("init");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[banner_view] %s", msg), core_msg);
    end;

    init_banner_list = function ()
        banner_view.inner_log("init_banner_list");

        WGBannerList:attach(banner_view.my_game_list_ctrl_id, banner_view.subscription_list_ctrl_id);
        banner_view.refresh_game_list(true);

        if true == banner_view.is_first_init then
            base_utility.add_interval("banner_view.refresh_for_no_game_tips", banner_view.get_list_time_out);
        end
    end;

    InitView = function()
        banner_view.pNoGame  = ui_utility.get_ctrl_ptr_by_path(banner_view.ub_game_list_panel_id_, 
                                "Ctn_GamesPnl\\pnlGameList\\pnlMyGameList\\pnlNoGame");
        local parent_id = ui_utility.get_ctrl_parent_id(banner_view.ub_game_list_panel_id_);
        banner_view.pLoading = ui_utility.get_ctrl_ptr_by_path(parent_id, "pnlLoading");
        banner_view.ptr_list_cover_mask_ = ui_utility.get_ctrl_ptr_by_path(parent_id, "GameListCoverMask");
        ui_utility.setproperty_by_id(banner_view.list_cover_mask_id_, "Visible", "false");
        if not banner_view.has_get_game_list then
            banner_view.ShowLoading(true);
            banner_view.ShowNoGame(false);
        else
            banner_view.ShowLoading(false);
        end
    end;

    focus_banner_item = function (module_id, ctrl_id)
        -- 模块id为game_id
        banner_view.inner_log("[focus_banner_item], game_id" .. module_id);
        -- 设置背景颜色
        local module_info = banner_model.get_banner_module_info_by_game_id(module_id);
        if module_info ~= nil and module_info.banner_color ~= nil and module_info.banner_color ~= "" then
            banner_view.inner_log("[focus_banner_item], banner_color:" .. tostring(module_info.banner_color));
            ui_utility.setproperty_by_cid(CID_WEGAME_DIALOG_LIST, "Background", module_info.banner_color);
        end
        -- 设置更多游戏菜单按钮的背景色
        if module_info ~= nil and module_info.color_mode == MainCommon.color_mode.kLight then
            -- 如果是浅色，则设置浅色的背景色
            ui_utility.setproperty_by_id(banner_view.more_menu_btn_id_, "Background", "COLOR:RGB:68,68,72;");
            ui_utility.setproperty_by_id(banner_view.more_menu_btn_id_, "BackgroundHover", "COLOR:RGB:63,63,67;");
        else
            -- 设置深色背景
            ui_utility.setproperty_by_id(banner_view.more_menu_btn_id_, "Background", "COLOR:RGB:48,48,51;");
            ui_utility.setproperty_by_id(banner_view.more_menu_btn_id_, "BackgroundHover", "COLOR:RGB:48,48,51;");
        end

        WGBannerList:change_selected_game(module_id, ctrl_id);
    end;

    unfocus_banner_item = function (module_id)
        if nil == module_id then
            banner_view.inner_log("[unfocus_banner_item]module_id is nil");
            return;
        end
        banner_view.inner_log(string.format("[unfocus_banner_item]game_id:%d", module_id));
       
        WGBannerList:selected_game(module_id, false);
    end;

    -- 更新游戏列表
    refresh_game_list = function(refresh_all_nodes)
        banner_view.inner_log("refresh_game_list");

        local game_list = banner_model.visible_game_list;

        banner_view.inner_log(string.format(
            "[refresh_game_list] game_list.count = %d", game_list.count));

--        game_list = nil;
--        game_list = {};
--        game_list.count = 0;
        if game_list.count > 0 then
            banner_view.has_get_game_list = true;
            banner_view.ShowLoading(false);
        end

        WGBannerList:refresh_list(refresh_all_nodes);

        if nil ~= banner_view.my_game_list_ctrl_id then
            if true == banner_view.has_get_game_list then
                local show_no_game_guide = false;
                if 0 == game_list.count then
                    banner_view.inner_log("show_guide");
                    show_no_game_guide = true;
                else
                    banner_view.inner_log("hide_guide");
                    show_no_game_guide = false;
                end
                banner_view.ShowLoading(false);
                banner_view.ShowNoGame(show_no_game_guide);
            end
        end

        banner_view.refresh_subscription_list();
    end;

    -- 更新游戏基本信息
    update_all_game_base_info = function ()
        banner_view.inner_log("refresh_all_base_data");
        WGBannerList:refresh_base_data(0);
    end;

    -- 更新游戏状态
    refresh_game_state = function(game_id)
        banner_view.inner_log(string.format("[refresh_state]game_id:%d", game_id));
        WGBannerList:refresh_state(game_id);
    end;

    -- 更新预更新游戏状态
    refresh_preupdate_state = function(game_id)
        WGBannerList:refresh_preupdate_state(game_id);
    end;

    get_current_select_module = function()
        return WGBannerList:get_current_select_module();
    end;

    refresh_for_no_game_tips = function ()
        banner_view.inner_log("[refresh_for_no_game_tips]");
        base_utility.remove_interval("banner_view.refresh_for_no_game_tips");
        banner_view.has_get_game_list = true;
        banner_model.op_from_my_app_list = true;
        banner_view.refresh_game_list(true);
    end;

    notify_game_list_ready = function ()
        banner_view.inner_log("notify_game_list_ready");
        banner_view.has_get_game_list = true;
        banner_view.refresh_game_list(true);
        -- 查询预约列表
        comm_center.call_service("Svr_UpdateSubscriptionList", {});
    end;

    -- 显示下架引导
    show_off_shell_tips = function (game_id, game_type, game_server_id, game_ver)
        banner_view.inner_log(string.format(
            "[show_off_shell_tips]game_id:%d, game_type:%d, server_id:%d, ver:%s",
            game_id, game_type, game_server_id, game_ver));
        local tips_ctrl_id = banner_model.get_off_shell_tips_ctrl_id(game_id);
        local tips = nil;
        if 0 == tips_ctrl_id then
            local ctrl_id = layout_mgr.restore_layout_multi("GameOffShellTips.tpf");
            tips = GameOffShellTips:new(ctrl_id, game_id);
            banner_model.ctrl_id_to_off_shell_tips[ctrl_id] = tips;
        else
            tips = banner_model.get_off_shell_tips(tips_ctrl_id);
        end

        if nil ~= tips then
            tips:set_game_info(game_type, game_server_id, game_ver);
            tips:show(true);
        end
    end;

    -- 显示更新时磁盘空间过少的提示
    show_lack_space_tips = function (game_id, server_id)
        banner_view.inner_log(string.format(
            "[show_lack_space_tips]game_id:%d, server_id:%d", game_id, server_id));
        local tips_ctrl_id = banner_model.get_lack_space_tips_ctrl_id(game_id);
        local tips = nil;
        if 0 == tips_ctrl_id then
            local ctrl_id = layout_mgr.restore_layout_multi("GameUpdateLackSpaceTips.tpf");
            tips = GameUpdateLackSpaceTips:new(ctrl_id, game_id);
            banner_model.ctrl_id_to_lack_space_tips[ctrl_id] = tips;
        else
            tips = banner_model.get_lack_space_tips(tips_ctrl_id);
        end

        if nil ~= tips then
            tips:set_update_info(server_id);
            tips:show(true);
        end
    end;

    -- 显示没有游戏的提示
    ShowNoGame = function(bShow)
        banner_view.showing_no_game = bShow;
        if banner_view.is_my_game_list_show() then
            banner_view.show_my_game_list(true); -- re-update layout
        else
            if banner_view.showing_no_game then
                ui_utility.setproperty_by_id(banner_view.no_game_panel_ctrl_id, "Visible", "true");
            else
                ui_utility.setproperty_by_id(banner_view.no_game_panel_ctrl_id, "Visible", "false");
            end
        end
    end;

    ShowLoading = function(bShow)
        if (nil ~= banner_view.pLoading) then
            if bShow then
                banner_view.pLoading:Show(TRUE);
            else
                banner_view.pLoading:Show(FALSE);
            end;
        end;
    end;

    show_list_cover_mask = function(bShow)
        if banner_view.ptr_list_cover_mask_ ~= nil then
            if bShow then
                banner_view.ptr_list_cover_mask_:Show(TRUE);
            else
                banner_view.ptr_list_cover_mask_:Show(FALSE);
            end
        end
    end;    

    pLoading = nil;
    pNoGame  = nil;

    init_banner_ctrl = function(parent_ctrl) -- pnlGameList
        banner_view.inner_log("init banner ctrls");

        banner_view.game_list_ctrl_id = parent_ctrl;

        local ctrl_id = ui_utility.get_ctrl_id_by_path(parent_ctrl, "pnlMyGameList");
        if Tenio.INVALID_CONTROLID == ctrl_id then
            banner_view.inner_log("get my game list container ctrl id failed", true);
        end
        banner_view.my_game_panel_ctrl_id = ctrl_id;

        ctrl_id = ui_utility.get_ctrl_id_by_path(parent_ctrl, "pnlMyGameList\\GameListSwitch");
        if Tenio.INVALID_CONTROLID == ctrl_id then
            banner_view.inner_log("get my game list switcher ctrl id failed", true);
        end
        banner_view.my_game_list_switcher_id = ctrl_id;

        ctrl_id = ui_utility.get_ctrl_id_by_path(parent_ctrl, "pnlMyGameList\\GameList");
        if Tenio.INVALID_CONTROLID == ctrl_id then
            banner_view.inner_log("get my game list ctrl id failed", true);
        end
        banner_view.my_game_list_ctrl_id = ctrl_id;

        ctrl_id = ui_utility.get_ctrl_id_by_path(parent_ctrl, "pnlMySubscriptionList");
        if Tenio.INVALID_CONTROLID == ctrl_id then
            banner_view.inner_log("get my subs list container ctrl id failed", true);
        end
        banner_view.subscription_panel_ctrl_id = ctrl_id;

        ctrl_id = ui_utility.get_ctrl_id_by_path(parent_ctrl, "pnlMySubscriptionList\\SubscriptionListSwitch");
        if Tenio.INVALID_CONTROLID == ctrl_id then
            banner_view.inner_log("get my subs list switcher ctrl id failed", true);
        end
        banner_view.subscription_list_switcher_id = ctrl_id;

        ctrl_id = ui_utility.get_ctrl_id_by_path(parent_ctrl, "pnlMySubscriptionList\\SubscriptionList");
        if Tenio.INVALID_CONTROLID == ctrl_id then
            banner_view.inner_log("get my subs list ctrl id failed", true);
        end
        banner_view.subscription_list_ctrl_id = ctrl_id;

        ctrl_id = ui_utility.get_ctrl_id_by_path(parent_ctrl, "pnlMyGameList\\pnlNoGame");
        if Tenio.INVALID_CONTROLID == ctrl_id then
            banner_view.inner_log("get nogame panel ctrl id failed", true);
        end
        banner_view.no_game_panel_ctrl_id = ctrl_id;

        ctrl_id = ui_utility.get_ctrl_id_by_path(banner_view.subscription_list_switcher_id, "SubscriptionIndicator");
        if Tenio.INVALID_CONTROLID == ctrl_id then
            banner_view.inner_log("get indicator ctrl id failed", true);
        end
        banner_view.subscription_indicator_id = ctrl_id;

    end;

    refresh_subscription_list = function()
        
        local subscription_list = banner_model.get_visible_subscription_list();
        banner_view.inner_log(string.format(
            "[refresh_game_list] subsgame count = %d", #subscription_list));

        if #subscription_list > 0 then
            banner_view.show_subscription_panel_ctrl(true);
            if banner_view.is_my_game_list_show() then
                banner_view.show_my_game_list(true);
                banner_view.show_subscription_list(false);
            end
            banner_view.update_subscription_indicator();
        else
            banner_view.show_subscription_panel_ctrl(false);
            banner_view.show_subscription_list(false);
        end

        WGBannerList:refresh_subscription_list(subscription_list);

        if #subscription_list > 0 and 
            banner_view.is_banner_visible() and 
             banner_model.show_subscription_user_guide() then
             base_utility.add_interval("banner_view.delay_show_subscription_user_guide", 3000);
        end

        if banner_model.need_show_subscription_notify_tips() then
            base_utility.add_interval("banner_view.delay_show_subscription_tips", 2000);
        end

    end;

    show_my_game_list = function(show)
        
        -- group indicator
        local indicator_id = ui_utility.get_ctrl_id_by_path(banner_view.my_game_list_switcher_id,
                                                        "GroupIndicator");

        if true == show then
            ui_utility.setproperty_by_id(banner_view.my_game_list_ctrl_id, "Visible", "true");
            -- re-set list ctrl height
            local ww, wh = ui_utility.get_control_size_by_id(banner_view.game_list_ctrl_id);
            if 0 == wh then return end;
            local mygame_list_height = wh;
            local visible_subscription_list = banner_model.get_visible_subscription_list();
            if #visible_subscription_list > 0 then
                mygame_list_height = wh - banner_view.switch_ctrl_height;
            end
            ui_utility.setproperty_by_id(banner_view.my_game_panel_ctrl_id, "Height"
                                                                   , tostring(mygame_list_height));
            mygame_list_height = mygame_list_height - banner_view.switch_ctrl_height;
            ui_utility.setproperty_by_id(banner_view.my_game_list_ctrl_id, "Height", tostring(mygame_list_height));
            if banner_view.showing_no_game then
                ui_utility.setproperty_by_id(banner_view.no_game_panel_ctrl_id, "Visible", "true");
                ui_utility.setproperty_by_id(banner_view.no_game_panel_ctrl_id, "Height", tostring(mygame_list_height));
            else
                ui_utility.setproperty_by_id(banner_view.no_game_panel_ctrl_id, "Visible", "false");
            end

            ui_utility.setproperty_by_id(indicator_id,
                                    "Background", BANNER_VIEW_RES.BANNER_GROUP_EXPAND_INDICATOR);

        else
            ui_utility.setproperty_by_id(banner_view.my_game_list_ctrl_id, "Visible", "false");
            ui_utility.setproperty_by_id(banner_view.my_game_panel_ctrl_id, "Height"
                                                 , tostring(banner_view.switch_ctrl_height));
            if banner_view.showing_no_game then
                ui_utility.setproperty_by_id(banner_view.no_game_panel_ctrl_id, "Visible", "false");
            end

            ui_utility.setproperty_by_id(indicator_id,
                                    "Background", BANNER_VIEW_RES.BANNER_GROUP_COLLAPSE_INDICATOR);
        end
    end;

    show_subscription_list = function(show)
        local indicator_id = ui_utility.get_ctrl_id_by_path(banner_view.subscription_list_switcher_id,
                                                        "GroupIndicator");
        if true == show then
            -- show my game list ctrl
            ui_utility.setproperty_by_id(banner_view.subscription_list_ctrl_id, "Visible", "true");
            -- re-set list ctrl height
            local ww, wh = ui_utility.get_control_size_by_id(banner_view.game_list_ctrl_id);
            if 0 == wh then return end;
            local mygame_list_height = wh - banner_view.switch_ctrl_height;
            ui_utility.setproperty_by_id(banner_view.subscription_panel_ctrl_id, "Height"
                                                                    , tostring(mygame_list_height));
            mygame_list_height = mygame_list_height - banner_view.switch_ctrl_height;
            ui_utility.setproperty_by_id(banner_view.subscription_list_ctrl_id, "Height"
                                                                    , tostring(mygame_list_height));
            ui_utility.setproperty_by_id(indicator_id,
                                    "Background", BANNER_VIEW_RES.BANNER_GROUP_EXPAND_INDICATOR);
        else
            -- hide subscription list ctrl
            ui_utility.setproperty_by_id(banner_view.subscription_list_ctrl_id, "Visible", "false");
            ui_utility.setproperty_by_id(banner_view.subscription_panel_ctrl_id, "Height"
                                                        , tostring(banner_view.switch_ctrl_height));
            ui_utility.setproperty_by_id(indicator_id,
                                    "Background", BANNER_VIEW_RES.BANNER_GROUP_COLLAPSE_INDICATOR);
        end
    end;

    show_subscription_panel_ctrl = function(show)
        if true == show then
            ui_utility.setproperty_by_id(banner_view.subscription_panel_ctrl_id, "Visible", "true");
        else
            ui_utility.setproperty_by_id(banner_view.subscription_panel_ctrl_id, "Visible", "false");
        end
    end;

    update_subscription_indicator = function()
        local count = banner_model.get_subscription_state_changed_count();
        if count > 0 then
            ui_utility.setproperty_by_id(banner_view.subscription_indicator_id, "Visible", "true");
            comm_center.call_service("i_service_show_home_tab_flashing", {});  -- 显示tab红点
        else
            ui_utility.setproperty_by_id(banner_view.subscription_indicator_id, "Visible", "false");
            comm_center.call_service("i_service_hide_home_tab_flashing", {});  -- 隐藏tab红点
        end
    end;

    is_my_game_list_show = function()
        return ui_utility.is_visible(banner_view.my_game_list_ctrl_id);
    end;

    is_subscription_list_show = function()
        return ui_utility.is_visible(banner_view.subscription_list_ctrl_id);
    end;

    is_banner_visible = function()
        return ui_utility.is_visible(banner_view.my_game_panel_ctrl_id);
    end;

    delay_show_subscription_user_guide = function()
        local params = {
            guide_url = LoadStr("SUBSCRIPTION_GUIDE_URL");
            guide_type = "subscription_guide";
        };
        local ret = comm_center.call_service("i_service_show_simple_user_guide", params);
        if nil ~= ret and 1 == ret.result then
            banner_model.set_subscription_user_guide_showed();
            base_utility.remove_interval("banner_view.delay_show_subscription_user_guide");
        end
    end;

    delay_show_subscription_tips = function()

        base_utility.remove_interval("banner_view.delay_show_subscription_tips");

        if banner_view.subscription_tips_times == nil then
            banner_view.subscription_tips_times = 0;
        end

        if banner_view.subscription_tips_times > 5 then
            banner_view.subscription_tips_times = 0;
            return;
        end

        local delay_show = false;
        local subscription_list = banner_model.subscription_state_notify_list;
        for k, v in pairs(subscription_list) do
            local game_id = tonumber(k);
            if nil ~= game_id and true == v.show_tips then
                local title = "";
                local action = "";
                local action_title = "";
                local state = banner_model.get_app_state(game_id);
                local info = banner_model.get_subscription_info(game_id);
                if nil ~= info then
                    if subscription_state.kStatePurchasable == info.status then
                        title = LoadStr("ID_TPF_PURCHASA");
                        action = "purchase";
                        action_title = LoadStr("ID_TPF_PURCHASE_NOW");
                    elseif subscription_state.kStateDownload == info.status or
                           subscription_state.kStateMaintein == info.status then
                        title = LoadStr("ID_TPF_DOWNLOAD_AVAILABL");
                        action = "download";
                        action_title = LoadStr("ID_TPF_DOWNLOAD_NOW");
                        if state ~= ierd_tgp.game_library.GAME_STATE_NOT_INSTALLED then
                            action = "landing";
                        end
                        if state == ierd_tgp.game_library.GAME_STATE_DOWNLOADING then
                            action_title = LoadStr("ID_TPF_DOWNLOADING");
                        end
                    elseif subscription_state.kStateRelease == info.status then
                        title = LoadStr("ID_TPF_RELEASED");
                        action = "play";
                        action_title = LoadStr("ID_TPF_PLAY_NOW");
                        if state ~= ierd_tgp.game_library.GAME_STATE_NORMAL then
                            action = "landing";
                        end
                    end
                    
                    if state == 0 then
                        action = "";
                        base_utility.log("[banner_view]unknown subscribed app status");
                    end
                end
                local base_info = banner_model.get_game_base_info(game_id);
                if "" ~= action and nil ~= base_info then
                   local params = {
                        game_id = game_id;
                        game_img_url = base_info.poster_url;
                        main_title = title;
                        third_title = action_title;
                        game_name = base_info.name;
                        game_action = action;
                    };
                    comm_center.call_service("i_service_show_subscription_notify_tips", params);
                    banner_model.set_subscription_state_notified(game_id);
                else
                    delay_show = true;
                end
            end
        end

        if true == delay_show then
            -- delay show
            banner_view.subscription_tips_times = banner_view.subscription_tips_times + 1;
            base_utility.add_interval("banner_view.delay_show_subscription_tips", 1000);
        else
            banner_view.subscription_tips_times = 0;
        end

    end;

    on_wnd_poschanged = function()
        banner_view.check_game_list_ctrl_layout();
    end;

    check_game_list_ctrl_layout = function()
        
        local is_game_list_show = banner_view.is_my_game_list_show();
        local is_subscription_show = banner_view.is_subscription_list_show();

        if not is_game_list_show and not is_subscription_show then
            local ww, wh = ui_utility.get_control_size_by_id(banner_view.my_game_panel_ctrl_id);
            if wh > banner_view.switch_ctrl_height then
                is_game_list_show = true;
            end
            
            if not is_game_list_show then
                ww, wh = ui_utility.get_control_size_by_id(banner_view.subscription_panel_ctrl_id);
                if wh > banner_view.switch_ctrl_height then
                    is_subscription_show = true;
                end
            end
        end

        if is_game_list_show then
            banner_view.show_my_game_list(true);
            banner_view.show_subscription_list(false);
        elseif is_subscription_show then
            banner_view.show_my_game_list(false);
            banner_view.show_subscription_list(true);
        end

    end;

};

banner_view.init();